package org.jeecg.modules.sanitation2.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.basic.demo.bed.entity.BedAndApart;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.sanitation2.entity.LwgySettingSanitation;
import org.jeecg.modules.sanitation2.entity.StatisticalQuery;
import org.jeecg.modules.sanitation2.service.ILwgySettingSanitationService;
import org.jeecg.modules.sanitation2.util.DateUtil;
import org.jeecg.modules.sanitation2.util.UnZipFilesUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.sanitation2.entity.LwgyTargetType;
import org.jeecg.modules.sanitation2.entity.LwgyScoreSanitation;
import org.jeecg.modules.sanitation2.vo.LwgyScoreSanitationPage;
import org.jeecg.modules.sanitation2.service.ILwgyScoreSanitationService;
import org.jeecg.modules.sanitation2.service.ILwgyTargetTypeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 卫生打分
 * @Author: jeecg-boot
 * @Date:   2021-04-29
 * @Version: V1.0
 */
@Api(tags="卫生打分")
@RestController
@Controller("testcontroller")
@RequestMapping("/sanitation2/lwgyScoreSanitation")
@Slf4j
public class LwgyScoreSanitationController {
	@Autowired
	private ILwgyScoreSanitationService lwgyScoreSanitationService;
	@Autowired
	private ILwgyTargetTypeService lwgyTargetTypeService;
	@Autowired
	private ILwgyBaseBedService lwgyBaseBedService;
	@Autowired
	private ILwgySettingSanitationService lwgySettingSanitationService;

	/* @Autowired
	 private ILwgyBaseApartmentService lwgyBaseApartmentService;
*/


	/**
	 * 分页列表查询
	 *
	 * @param lwgyScoreSanitation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "卫生打分-分页列表查询")
	@ApiOperation(value="卫生打分-分页列表查询", notes="卫生打分-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyScoreSanitation lwgyScoreSanitation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyScoreSanitation> queryWrapper = QueryGenerator.initQueryWrapper(lwgyScoreSanitation, req.getParameterMap());
		Page<LwgyScoreSanitation> page = new Page<LwgyScoreSanitation>(pageNo, pageSize);
		IPage<LwgyScoreSanitation> pageList = lwgyScoreSanitationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 /**
	  * 自定义分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "卫生2-分页列表查询")
	 @ApiOperation(value = "卫生2-分页列表查询", notes = "卫生2-分页列表查询")
	 @GetMapping(value = "/listCustomScore")
	 public Result<?> queryPageListCustomScore(LwgyScoreSanitation lwgyScoreSanitation,
											   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
											   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
											   @RequestParam(name = "scaleBatches",required = false) String scaleBatches,
											   @RequestParam(name = "studentCampus",required = false) String studentCampus,
											   @RequestParam(name = "studentCollege",required = false) String studentCollege,
											   HttpServletRequest req) {
		 LwgyBaseBed lwgyBaseBed =new LwgyBaseBed();
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> role = lwgyBaseBedService.getRoleById(sysUser.getId());
		 if (role.contains("fdy")) {//不是管理员，显示管理员分配的的床位
			 String workNo = sysUser.getWorkNo();
			 lwgyBaseBed.setAdminId(workNo);
		 }
		// LwgyScoreSanitation lwgyScoreSanitation=new LwgyScoreSanitation();
		 Page<LwgyScoreSanitation> page = new Page<LwgyScoreSanitation>(pageNo, pageSize);
		 IPage<LwgyScoreSanitation> ipage=null;
		 ipage = lwgyScoreSanitationService.queryPageListCustomScore(page,scaleBatches,studentCampus,studentCollege);
		 return Result.OK(ipage);
	 }


	/**
	 *   添加
	 *
	 * @param lwgyScoreSanitationPage
	 * @return
	 */
	@AutoLog(value = "卫生打分-添加")
	@ApiOperation(value="卫生打分-添加", notes="卫生打分-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage) {
		System.out.println("11:"+lwgyScoreSanitationPage.getLwgyTargetTypeList());
		LwgyScoreSanitation lwgyScoreSanitation = new LwgyScoreSanitation();
		BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyScoreSanitation);
		lwgyScoreSanitationService.saveMain(lwgyScoreSanitation, lwgyScoreSanitationPage.getLwgyTargetTypeList());
		System.out.println("22:"+lwgyScoreSanitationPage.getLwgyTargetTypeList());
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param lwgyScoreSanitationPage
	 * @return
	 */
	@AutoLog(value = "卫生打分-编辑")
	@ApiOperation(value="卫生打分-编辑", notes="卫生打分-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage) {
		LwgyScoreSanitation lwgyScoreSanitation = new LwgyScoreSanitation();
		BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyScoreSanitation);
		LwgyScoreSanitation lwgyScoreSanitationEntity = lwgyScoreSanitationService
				.getById(lwgyScoreSanitation.getId());
		if(lwgyScoreSanitationEntity==null) {
			return Result.error("未找到对应数据");
		}
		lwgyScoreSanitationService.updateMain(lwgyScoreSanitation, lwgyScoreSanitationPage.getLwgyTargetTypeList());
		return Result.OK("编辑成功!");
	}
	 /**
	  *  编辑
	  *
	  * @param lwgyScoreSanitationPage
	  * @return
	  */


	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "卫生打分-通过id删除")
	@ApiOperation(value="卫生打分-通过id删除", notes="卫生打分-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyScoreSanitationService.delMain(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "卫生打分-批量删除")
	@ApiOperation(value="卫生打分-批量删除", notes="卫生打分-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyScoreSanitationService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "卫生打分-通过id查询")
	@ApiOperation(value="卫生打分-通过id查询", notes="卫生打分-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyScoreSanitation lwgyScoreSanitation = lwgyScoreSanitationService.getById(id);
		if(lwgyScoreSanitation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyScoreSanitation);

	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "打分指标通过主表ID查询")
	@ApiOperation(value="打分指标主表ID查询", notes="打分指标-通主表ID查询")
	@GetMapping(value = "/queryLwgyTargetTypeByMainId")
	public Result<?> queryLwgyTargetTypeListByMainId(@RequestParam(name="id",required=true) String id) {
		List<LwgyTargetType> lwgyTargetTypeList = lwgyTargetTypeService.selectByMainId(id);
		return Result.OK(lwgyTargetTypeList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyScoreSanitation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyScoreSanitation lwgyScoreSanitation) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<LwgyScoreSanitation> queryWrapper = QueryGenerator.initQueryWrapper(lwgyScoreSanitation, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<LwgyScoreSanitation> queryList = lwgyScoreSanitationService.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<LwgyScoreSanitation> lwgyScoreSanitationList = new ArrayList<LwgyScoreSanitation>();
      if(oConvertUtils.isEmpty(selections)) {
          lwgyScoreSanitationList = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          lwgyScoreSanitationList = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<LwgyScoreSanitationPage> pageList = new ArrayList<LwgyScoreSanitationPage>();
      for (LwgyScoreSanitation main : lwgyScoreSanitationList) {
          LwgyScoreSanitationPage vo = new LwgyScoreSanitationPage();
          BeanUtils.copyProperties(main, vo);
          List<LwgyTargetType> lwgyTargetTypeList = lwgyTargetTypeService.selectByMainId(main.getId());
          vo.setLwgyTargetTypeList(lwgyTargetTypeList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "卫生打分列表");
      mv.addObject(NormalExcelConstants.CLASS, LwgyScoreSanitationPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("卫生打分数据", "导出人:"+sysUser.getRealname(), "卫生打分"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<LwgyScoreSanitationPage> list = ExcelImportUtil.importExcel(file.getInputStream(), LwgyScoreSanitationPage.class, params);
              for (LwgyScoreSanitationPage page : list) {
                  LwgyScoreSanitation po = new LwgyScoreSanitation();
                  BeanUtils.copyProperties(page, po);
                  lwgyScoreSanitationService.saveMain(po, page.getLwgyTargetTypeList());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
    }


	/**
	* 上传离线打分数据
	*/
	 @AutoLog(value = "上传离线打分数据")
	 @ApiOperation(value="上传离线打分数据", notes="上传离线打分数据")
	 @PostMapping("/uploadOfflineData")
	 public Result<?>uploadOfflineData(@RequestBody List<LwgyScoreSanitationPage> lwgyScoreSanitationPages){
		LwgyScoreSanitationPage lwgyScoreSanitationPage = new LwgyScoreSanitationPage();//
		lwgyScoreSanitationPage.setId("123");
		 lwgyScoreSanitationPage.setCampusId("1");
		lwgyScoreSanitationPage.setBuildingId("12");
		lwgyScoreSanitationPage.setUnitId("59");
		 lwgyScoreSanitationPage.setLayerId("146");
		 lwgyScoreSanitationPage.setRoomId("2231");
		 lwgyScoreSanitationPage.setTotalScore("100");
		 lwgyScoreSanitationPage.setScaleBatches("1372371248686383106");
		lwgyScoreSanitationPage.setScenePhoto(lwgyScoreSanitationPage.getScenePhoto());
		List<LwgyTargetType> lwgyTargetTypes = new ArrayList<>(2);
		LwgyTargetType lwgyTargetType = new LwgyTargetType();
		lwgyTargetType.setSanitationId(lwgyScoreSanitationPage.getId());
		lwgyTargetType.setId("1231");
		lwgyTargetTypes.add(lwgyTargetType);							//构建打分指标
		lwgyScoreSanitationPage.setLwgyTargetTypeList(lwgyTargetTypes);
	 	lwgyScoreSanitationPages.add(lwgyScoreSanitationPage);			//构建打分VO
		 List<LwgyTargetType> lwgyTargetTypeLists = new ArrayList<>(20);
		 LwgyScoreSanitation lwgyScoreSanitation = null;
		 List<LwgyScoreSanitation> lwgyScoreSanitations = new ArrayList<>(15);
		 for (LwgyScoreSanitationPage scoreSanitationPage : lwgyScoreSanitationPages) {
			 List<LwgyTargetType> lwgyTargetTypeList = scoreSanitationPage.getLwgyTargetTypeList();//提取打分指标
			 lwgyTargetTypeLists.addAll(lwgyTargetTypeList);						//放入单独集合类
			 lwgyScoreSanitation = new LwgyScoreSanitation();
			 BeanUtils.copyProperties(scoreSanitationPage, lwgyScoreSanitation);//逐个取出打分列表内容放入打分实体类
			 lwgyScoreSanitations.add(lwgyScoreSanitation);						//构建新的打分实体类集合
		 }
	 	lwgyScoreSanitationPages.forEach(System.out::println);
		 System.out.println("============================");
		 lwgyTargetTypeLists.forEach(System.out::println);
		 lwgyScoreSanitationService.saveBatch(lwgyScoreSanitations);//批量保存打分集合
		 lwgyTargetTypeService.saveBatch(lwgyTargetTypeLists); 		//批量保存打分指标各项成绩
	 	return Result.OK("打分成绩上传成功");
	 }
	 /**
	 *上传离线打分单元测试
	 */
	 @Test
	 public void test(){
		 List<LwgyScoreSanitationPage> lwgyScoreSanitationPages = new ArrayList<>(2);
		 LwgyScoreSanitationPage lwgyScoreSanitationPage = new LwgyScoreSanitationPage();
		 lwgyScoreSanitationPage.setId("000");
		 lwgyScoreSanitationPage.setBuildingId("000");
		 List<LwgyTargetType> lwgyTargetTypes = new ArrayList<>(2);
		 LwgyTargetType lwgyTargetType = new LwgyTargetType();
		 lwgyTargetType.setSanitationId(lwgyScoreSanitationPage.getId());
		 lwgyTargetType.setId("0000");
		 lwgyTargetTypes.add(lwgyTargetType);
		 lwgyScoreSanitationPage.setLwgyTargetTypeList(lwgyTargetTypes);
		 lwgyScoreSanitationPages.add(lwgyScoreSanitationPage);
		 uploadOfflineData(lwgyScoreSanitationPages);
	 }

	 @AutoLog(value = "解压图片")
	 @ApiOperation(value="解压图片", notes="解压图片")
	 @PostMapping(value = "/analysisFile")
	 public void analysisFile(HttpServletRequest request) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 try {
			 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
			 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
				 MultipartFile multipartFile = entity.getValue();// 获取上传文件对象
				  List<String> filePathList = UnZipFilesUtil.batchadd(multipartFile );//调用解压方法并获取解压出来的文件地址
			 }
		 } catch (Exception e) {
			 log.error("文件解析失败！");
			 //return Result.Error("文件解析失败！");
		 }
	 }
	 @AutoLog(value = "生成压缩包")
	 @ApiOperation(value="生成压缩包", notes="生成压缩包")
	 @PostMapping(value = "/multipart")
	 public Result<?> multipart( HttpServletRequest request ) {
	 	MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest) request;
	 	MultipartFile multipartFile = mpRequest.getFile("fileZip");
		 String t = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		 String path = "D:/lwserver32/gy/lxdf/" + DateUtil.getYear() + "/";
		 File pathFile = new File(path + multipartFile.getOriginalFilename());
		 if (!pathFile.exists() && !pathFile.isDirectory()) {
			 pathFile.mkdirs();
		 }
		 try {
			 //multipartFile.transferTo(pathFile);
			 this.lwgyScoreSanitationService.executeSql("INSERT INTO imagezip(name,time) VALUES(now(),now())");
		 } catch (IllegalStateException var9) {
			 var9.printStackTrace();
		 } catch (Exception var10) {
			 var10.printStackTrace();
		 }

		 return Result.OK("生成成功");

	 }

	 @AutoLog(value = "查询打分成绩")
	 @ApiOperation(value="查询打分成绩", notes="查询打分成绩")
	 @GetMapping("/statisticalQueryList")
	 public Result<?>statisticalQueryList(StatisticalQuery statisticalQuery,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										  HttpServletRequest req){
	 	QueryWrapper<StatisticalQuery> queryWrapper = QueryGenerator.initQueryWrapper(statisticalQuery,req.getParameterMap());
		 Page<LwgyScoreSanitation> page = new Page<>(pageNo, pageSize);
		 IPage<LwgyScoreSanitation> pageList = lwgyScoreSanitationService.statisticalQuery(page, queryWrapper);
	 	return Result.OK(pageList);
	 }

/*
	 @AutoLog(value = "上传离线打分数据_target容易有费数据")
	 @ApiOperation(value="上传离线打分数据_已测可用", notes="上传离线打分数据_已测可用")
	 @PostMapping("/insertScore")
	 public Result<?>insertScore(@RequestBody List<LwgyScoreSanitationPage> lwgyScoreSanitationPages){
	 	List<LwgyScoreSanitation> lwgyScoreSanitations = new ArrayList<>();
		 List<LwgyTargetType> lwgyTargetTypeLists = new ArrayList<>();
		 LwgyScoreSanitation lwgyScoreSanitation = null;

		 if(oConvertUtils.isNotEmpty(lwgyScoreSanitationPages) && lwgyScoreSanitationPages.size()>0){
			 for (LwgyScoreSanitationPage scoreSanitationPage : lwgyScoreSanitationPages){
				 List<LwgyTargetType> lwgyTargetTypeList = scoreSanitationPage.getLwgyTargetTypeList();//提取打分指标
				 lwgyScoreSanitation = new LwgyScoreSanitation();
				 String scoreId = UUIDGenerator.generate();

				 lwgyScoreSanitation.setId(scoreId);
				 lwgyTargetTypeList.stream().forEach(p->{
					 String targetId = UUIDGenerator.generate();
					 p.setSanitationId(scoreId);
					 p.setId(targetId);
				 });
				 lwgyScoreSanitation.setCampusId(scoreSanitationPage.getCampusId());
				 lwgyScoreSanitation.setBuildingId(scoreSanitationPage.getBuildingId());
				 lwgyScoreSanitation.setUnitId(scoreSanitationPage.getUnitId());
				 lwgyScoreSanitation.setLayerId(scoreSanitationPage.getLayerId());
				 lwgyScoreSanitation.setRoomId(scoreSanitationPage.getRoomId());
				 lwgyScoreSanitation.setTotalScore(scoreSanitationPage.getTotalScore());
				 lwgyScoreSanitation.setScaleBatches(scoreSanitationPage.getScaleBatches());
				 lwgyScoreSanitation.setScenePhoto(scoreSanitationPage.getScenePhoto());
				 lwgyScoreSanitations.add(lwgyScoreSanitation);
				 lwgyTargetTypeLists.addAll(lwgyTargetTypeList);
			 }

		 }
         lwgyScoreSanitationService.insertScoreInfo(lwgyScoreSanitations);
         lwgyTargetTypeService.insertTargetInfo(lwgyTargetTypeLists);//批量保存打分集合
		 return Result.OK("打分成绩上传成功");
	 }
*/

	 @AutoLog(value = "上传离线打分数据_ID")
	 @ApiOperation(value="上传离线打分数据_ID", notes="上传离线打分数据_已测可用")
	 @PostMapping("/insertScore")
	 public Result<?>insertScore(@RequestBody List<LwgyScoreSanitationPage> lwgyScoreSanitationPages){
		 System.out.println("开始进入上传离线打分============================================================");
		 List<LwgyScoreSanitation> lwgyScoreSanitations = new ArrayList<>();
		 List<LwgyTargetType> lwgyTargetTypeLists = new ArrayList<>();
		 LwgyScoreSanitation lwgyScoreSanitation = null;
		 String pcCode="";
		 //上传的数据大于非空
		 if(oConvertUtils.isNotEmpty(lwgyScoreSanitationPages) && lwgyScoreSanitationPages.size()>0){
			 LwgyScoreSanitationPage vo= lwgyScoreSanitationPages.get(0);
			 //本次打分pc
			 pcCode = vo.getScaleBatches();
			 System.out.println("pcCode:"+pcCode);
			 //查询本pc的之前打过的分数（总）
			 QueryWrapper<LwgyScoreSanitation> queryWrapper = new QueryWrapper<LwgyScoreSanitation>();
			 queryWrapper.eq("scale_batches",pcCode);
			 List<LwgyScoreSanitation> oldScoreList = lwgyScoreSanitationService.list(queryWrapper);
			 System.out.println("查询本pc的之前打过的分数（总）oldScoreList ===================" + oldScoreList);
			 //查询本pc的之前打过的分数（子）
			 List<String> ids = oldScoreList.stream().map(p -> p.getId()).collect(Collectors.toList());//先取出所有总的id
			 List<LwgyTargetType> oldTargetList = new ArrayList<LwgyTargetType>();
			 if(oConvertUtils.isNotEmpty(ids) && ids.size()>0){
				 QueryWrapper<LwgyTargetType> queryWrapper1 = new QueryWrapper<LwgyTargetType>();
				 queryWrapper1.in("sanitation_id",ids);
				 oldTargetList =  lwgyTargetTypeService.list(queryWrapper1);
			 }
			 System.out.println("查询本pc的之前打过的分数（子）oldTargetList ===================" + oldTargetList);

			 for (LwgyScoreSanitationPage scoreSanitationPage : lwgyScoreSanitationPages){
				 List<LwgyTargetType> lwgyTargetTypeList = scoreSanitationPage.getLwgyTargetTypeList();//提取打分指标
				 lwgyScoreSanitation = new LwgyScoreSanitation();
				 //判断原来有无次成绩，
				 String roomId = scoreSanitationPage.getRoomId();
				 System.out.println("判断原来有无次成绩================================================");
				 List<LwgyScoreSanitation> oldaList = oldScoreList.stream().filter(item->item.getRoomId().equals(roomId)).collect(Collectors.toList());
				 //原来该pc该房间打过成绩，原来的id取出，赋值给总成绩和子成绩sanitationId，且取出原子成绩id 赋值给子成绩
				 if(oConvertUtils.isNotEmpty(oldaList)&&oldaList.size()>0){
					 LwgyScoreSanitation score = oldaList.get(0);
					 lwgyScoreSanitation.setId(score.getId());
					 //lwgyTargetTypeList.stream().forEach(p->p.setSanitationId(score.getId()));
					 //子成绩的id=原系统中id,子系统中sanitationId 等于元系统中总成绩id
					 System.out.println("子成绩的id=原系统中id,子系统中sanitationId 等于元系统中总成绩id");
					 List<LwgyTargetType> finalOldTargetList = oldTargetList;
					 if(oConvertUtils.isNotEmpty(finalOldTargetList) && finalOldTargetList.size()>0){
						 lwgyTargetTypeList.stream().forEach(p->
								 finalOldTargetList.stream().forEach(t->{

									 String targetId = UUIDGenerator.generate();
									 p.setId(targetId);
									/* if(t.getSanitationId().equals(score.getId()) && t.getTargetName().equals(p.getTargetName())){
										 p.setId(t.getId());
									 }*/
									 p.setSanitationId(score.getId());
								 })
						 );
					 }else{//原打过总成绩但是没有子成绩
						 System.out.println("原打过总成绩但是没有子成绩");
						 lwgyTargetTypeList.stream().forEach(p->{
									 String targetId = UUIDGenerator.generate();
									 p.setId(targetId);
									 p.setSanitationId(score.getId());
								 }

						 );
					 }


				 }else{//原房间没有打过成绩，重新生成一个总成绩id
					 System.out.println("原房间没有打过成绩，重新生成一个总成绩id");
					 String scoreId = UUIDGenerator.generate();
					 lwgyScoreSanitation.setId(scoreId);
					 lwgyTargetTypeList.stream().forEach(p->{
						 p.setSanitationId(scoreId);
						 String targetId = UUIDGenerator.generate();
						 p.setId(targetId);
					 });
				 }
				 lwgyScoreSanitation.setCampusId(scoreSanitationPage.getCampusId());
				 lwgyScoreSanitation.setBuildingId(scoreSanitationPage.getBuildingId());
				 lwgyScoreSanitation.setUnitId(scoreSanitationPage.getUnitId());
				 lwgyScoreSanitation.setLayerId(scoreSanitationPage.getLayerId());
				 lwgyScoreSanitation.setRoomId(scoreSanitationPage.getRoomId());
				 lwgyScoreSanitation.setTotalScore(scoreSanitationPage.getTotalScore());
				 lwgyScoreSanitation.setScaleBatches(scoreSanitationPage.getScaleBatches());
				 lwgyScoreSanitation.setScenePhoto(scoreSanitationPage.getScenePhoto());
				 lwgyScoreSanitations.add(lwgyScoreSanitation);


				 lwgyTargetTypeLists.addAll(lwgyTargetTypeList);
			 }

		 }
		 System.out.println("lwgyScoreSanitations==============================================" + lwgyScoreSanitations);
		 System.out.println("批量保存打分集合lwgyTargetTypeLists==================================" + lwgyTargetTypeLists);
/*		 lwgyScoreSanitationService.saveOrUpdateBatch(lwgyScoreSanitations);//批量保存打分集合
		 lwgyTargetTypeService.saveOrUpdateBatch(lwgyTargetTypeLists); 		//批量保存打分指标各项成绩*/
		 lwgyScoreSanitationService.insertScoreInfo(lwgyScoreSanitations);
		 lwgyTargetTypeService.insertTargetInfo(lwgyTargetTypeLists);//批量保存打分集合
		 System.out.println("上传离线打分结束============================================================");
		 return Result.OK("打分成绩上传成功");
	 }

	 /**
	  * 批量确认成绩
	  *
	  * @param lwgyScoreSanitation
	  * @return
	  */
	 @AutoLog(value = "批量确认成绩")
	 @ApiOperation(value = "批量确认成绩", notes = "批量确认成绩")
	 @PostMapping(value = "/gradeRecognition")
	 public Result<?> gradeRecognition(@RequestBody LwgyScoreSanitation lwgyScoreSanitation) {
		 String scaleBatches = lwgyScoreSanitation.getScaleBatches();
		 // 成功确认打分批次
		 LwgySettingSanitation lwgySettingSanitation = new LwgySettingSanitation();
		 lwgySettingSanitation.setId(scaleBatches);
		 lwgySettingSanitationService.updateStateById(lwgySettingSanitation);

		 List<LwgyScoreSanitation> scoreList = lwgyScoreSanitationService.scoreSearch(scaleBatches);
		 List<LwgyScoreSanitation> list = new ArrayList<>();
		 scoreList.stream().forEach(item -> {
			 LwgyScoreSanitation lwgyScore = new LwgyScoreSanitation();
			 lwgyScore.setId(item.getId());
			 lwgyScore.setSuccessState("1");
			 list.add(lwgyScore);
		 });
		 // 批量确认成绩
		 lwgyScoreSanitationService.updateBatchById(list);
		 return Result.OK("批量确认成功!");
	 }
 }
